import requests
import os.path
import urllib.parse
import logging
import logging.config


class Sea:
    '''将 sea 文件保存 seafile,二级目录设计'''
    def __init__(self,server_url):
        self.session = requests.Session()
        self.server_url = server_url
        
    def put(self,name,path,data,filename=None,content_type='application/octet-stream'):
        file_path = urllib.parse.urljoin(self.server_url,os.path.join(path,name))
        
        # 导入到服务, 直到成功
        files = {'file': (filename or name, data, content_type)}
        while 1:
            try:
                response = self.session.post(file_path,files=files)
                if response.status_code >= 200 and response.status_code<300:
                    return file_path
                else:
                    logging.info('status no 200:',response.status_code)
            except Exception as e:
                logging.warning(e,response.text)
    
    def get(self,name,path):
        '''503 未处理'''
        file_path = urllib.parse.urljoin(self.server_url,os.path.join(path,name))

        while 1:
            try:
                response = self.session.get(file_path,timeout=30)
                if response.status_code >= 200 and response.status_code<300:
                    html_data = response.content
                    content_type = response.headers['Content-Type']
                    try:
                        head_filename = response.headers['Content-Disposition'].encode('ISO-8859-1').decode('utf8')
                        filename = head_filename.replace('inline; filename=','').strip('"')
                    except Exception as e:
                        filename = ''

                    return filename,content_type,html_data

                elif response.status_code == 404:
                    return None,None,None
        
                logging.warning('status no 200:',response.status_code,response.text)
            except Exception as e:
                logging.info(e)
                
    def delete(self,name,path):
        '''503 未处理'''
        file_path = urllib.parse.urljoin(self.server_url,os.path.join(path,name))

        response = self.session.delete(file_path)
        while 1:
            if response.status_code >= 200 and response.status_code<300:
                return True
            elif response.status_code == 500:
                return True
            logging.info('status no 200:',response.status_code)

    def head(self,name,path):
        '''503 未处理'''
        file_path = urllib.parse.urljoin(self.server_url,os.path.join(path,name))

        response = self.session.head(file_path)
        while 1:
            if response.status_code >= 200 and response.status_code<300:
                return True
            elif response.status_code == 404:
                return False
            logging.info('status no 200:',response.status_code)


SEA = Sea('http://192.168.1.223:8880')


with open('/mnt/c/Users/32673/Pictures/g_1.jpg','rb')as f:
    data = f.read()
    SEA.put('g_1.jpg','pic',data)